<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '160px' : '160px'">
        <el-col :span="24">
          <!-- 表单类型 0-设计器生成 1-自定义代码 -->
          <el-form-item label="数据库建模方式">
            <!--文本框-->
            <el-radio-group v-model="modelFlagType" @change="modelFlagChange">
              <el-radio-button label="0">新建模型</el-radio-button>
              <el-radio-button label="1">选择已有的模型</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="modelCodeShow" :span="24">
          <!-- 表单类型 0-设计器生成 1-自定义代码 -->
          <el-form-item label="已有的数据库模型"
                        prop="modelId">
            <!--文本框-->
            <el-select v-model="dataForm.modelId" placeholder="选择数据库" @change="tableSelect" clearable>
              <el-option v-for="table in this.tableList"
                         :key="table.id"
                         :label="table.tableNameAndComment"
                         :value="table.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="confirmBtn()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      modelCodeShow: false,
      modelFlagType: '0',
      tableList: [],
      formData: [],
      dataForm: {
        id: '',
        modelFlag: true,
        modelCode: '',
        modelId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        modelId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.loading = true
        this.getTableList()
        this.getInfo()
      })
    },
    modelFlagChange (val) {
      if (val === '0') {
        this.modelCodeShow = false
      } else {
        this.modelCodeShow = true
      }
    },
    tableSelect (val) {
      if (val) {
        let tableVal = this.$xeutils.find(this.tableList, item => item.id === val)
        this.dataForm.modelCode = tableVal.tableName
      }
    },
    getTableList () {
      this.$http.get(`/dpd/dpdTable/list`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.tableList = res.data
      }).catch(() => {})
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/drs/drsFormDef/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.formData = res.data
      }).catch(() => {})
    },
    confirmBtn () {
      if (this.modelFlagType === '1') {
        this.dataFormSubmitHandle()
      } else {
        this.visible = false
        this.$emit('openTableDesign', true, this.formData)
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/drs/drsFormDef/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('openTableDesign', false)
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
